//常用图片
var picUserHead = "/ApiCloud/assets/img/userHead.jpeg"


//APP部署环境设置
var AppCodeEnv = 1;//0=LOCAL;1=SEVER
var AppDevEvn = 1;//0=Prod;1=DEV

//数据配置
var version = "1.00";
var VI = "714a08411a995a07bc5fa247f5f66bc5";

//网络请求配置
// var pageUrl = "http://192.168.0.5:8081";
// var pageApi = "http://192.168.0.5:8081";
// var baseUrl = "http://192.168.0.5:83";
// var baseApi = "http://192.168.0.5:83";
//
// var loggerUrl = "http://192.168.0.5:4237";


//网络请求配置-郭
// var pageUrl = "http://photo.zhencaidiy.com";
// var pageApi = "http://photo.zhencaidiy.com";
var baseUrl = "";
var baseApi = "";
//
// var loggerUrl = "http://127.0.0.1";
//
//
// var pageUrl = "http://192.168.1.5:8081";
// var pageApi = "http://192.168.1.5:8081";
// var baseUrl = "http://192.168.1.5:83";
// var baseApi = "http://192.168.1.5:83";
// var loggerUrl = "http://192.168.1.5:4237";

// var pageUrl = "http://192.168.43.207:8081";
// var pageApi = "http://192.168.43.207:8081";
// var baseUrl = "http://192.168.43.207:82";
// var baseApi = "http://192.168.43.207:82";



// var baseUrl = "http://photo.sano100.com";
// var baseApi = "http://photo.sano100.com";

//图片上传设置，需要在页面加载相应JS
var toOSS = 1;//0:上传到服务器，1：上传到OSS
var ossUrl = 'http://sano100-tjb.oss-cn-beijing.aliyuncs.com';


//*********基础功能重新封装**************//
function getAssetBaseUrl() {
    if (toOSS == 1) {
        return ossUrl
    }else {
        return baseUrl
    }
}

//信息输出
var MyAppLogStatus = 2;

function isFalse(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    return true
}
function isTrue(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    return true
}

function logHtml(info) {
    // apiLog(info)
}

var log  = function (info){
    if(MyAppLogStatus == 1){
        console.log(info);
    }
    if(MyAppLogStatus == 2){
        logHtml(
            "==========================================" + "<br>" + "caller:" + log.caller+ "<br>"
            + "--------------------------------------------------------------------------" + "<br>"
            + info + "<br>"
            + "==========================================" + "<br><br><br><br>"
        );
    }
}
function logJson(info){
    if(MyAppLogStatus == 1){
        console.log(JSON.stringify(info));
    }
    if(MyAppLogStatus == 2){
        logHtml(
            "==========================================" + "<br>" + "caller:" + log.caller + "<br>"
            + "------------------------------------------------------------------------" + "<br>"
            + JSON.stringify(info) + "<br>"
            + "==========================================" + "<br><br><br><br>"
        );
    }
}

//打印对象
function dump(obj, index) {
  // appcan.logs(typeof obj)
  if (typeof obj == "object") {
    for (var i in obj) {
      dump(obj[i], i)
    }
  } else {
      console.log(index + ":" + obj + "(" + typeof obj + ")")
  }
}

//URL中的参数转对象
function getDataFromUrl(data){
    var queryArr = data.split("?");
    return queryArr[1];
}
function getDataFromQuery(data){
    var result = {};
    var queryArr = data.split("&");
    queryArr.forEach(function(item){
        var value = item.split("=")[1];
        var key = item.split("=")[0];
        result[key] = value;
    });
    return result;
}

//存储本地信息
function setLocData(key,value){
    // delLocData(key)
    // if(typeof value != "string"){
    //     value = JSON.stringify(value);
    // }
    // api.setPrefs({
    //     key: key,
    //     value: value
    // });
    // appcan.locStorage.setVal(key,value)
    localStorage.setItem(key,value)
}

//获取本地数据
function getLocData(key){
    return localStorage.getItem(key)
    //同步返回结果：
    // var data = api.getPrefs({
    //     sync: true,
    //     key: key
    // });
    // if( data !== ""){
    //     try {
    //         var value = JSON.parse(data);
    //     }catch (e) {
    //         value = ""
    //     }finally {
    //
    //     }
    //     return value
    // }
    // return data;
}

//删除本地数据
function delLocData(key){
    // api.removePrefs({
    //     key: key
    // });
}

//删除所有本地数据
function delAllLocData(){
    api.clearCache(function() {
        api.toast({
            msg: '清除完成'
        });
    });
}

//关于数据传递
function waitFor(targetWindowName,func)
{
    log("iam waitFor")
    appcan.window.subscribe("WaitFor" + targetWindowName , function (msg) {
        try{
            var data = JSON.parse(msg)
        }catch (e){
            showToast("订阅信息类型错误")
        }finally{
        }
        func(data)
    })
}
function callOn(windowName,data)
{
    log("iam callOn")
    if (typeof data != "string") {
        data = JSON.stringify(data)
    }
    appcan.window.publish("WaitFor" + windowName ,data)
}






//点击事件
function ontap(seletor,func){
    appcan.button(seletor,"ani-act",func);
}

function mkSelect (selector,data,onChange) {
    try{
        var option = "";
        data.map(function (d) {
            option = option + '<option value="' + d.id + '"  >' + d.name + '</option>';
        });
        $(selector + " select").append(option);
        appcan.select(selector,onChange);
    }catch (e){
        showToast("select error")
    }finally{

    }
}

function commDatePicker(_this){
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    uexControl.openDatePicker(y,m,d, function(data) {
        var today = data.year + "-" + data.month + "-" + data.day;
        _this.val(today)
    });
}
function commTimePicker(_this){
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var i = date.getMinutes();
    uexControl.openTimePicker(h,i, function(data) {
        //var today = data.year + "-" + data.month + "-" + data.day;
        var time = data.hour + ":" + data.minute
        _this.val(time)
    });
}

//******************用户相关功能******************
function getUser(){
    var user = getLocData("user");
    try{
        user = JSON.parse(user);
    }catch(e){
        return "";
    }finally{}
    return user;
}

function getUid(){
    var user = getUser();
    return user.id
}

function chkUser(){
    try{
        var uid = getUid();
    }catch(e){
        goto("userLogin",{});
        return;
    }finally{}
    if(uid == undefined){
        goto("userLogin",{});
        return;
    }
    return uid;
}

//**********************图片展示相关功能*********************
function mkPicStyle4BackGround(pic){
    return "background-image: url('" + pic + "')"
}


// function arrayDelIndex(array,index) {
//     var tmp = [];
//     array.map(function (data,i) {
//         if (i != index) {
//             tmp.push(data)
//         }
//     })
//     return tmp;
// }
//
// function arrayDelValue(array,value) {
//     var tmp = [];
//     array.map(function (data,i) {
//         if (data != value) {
//             tmp.push(data)
//         }
//     })
//     return tmp;
// }



function objDelIndex(obj, index) {
    var tmp = {};
    for (var i in obj) {
        if (i != index) {
            tmp[i] = obj[i]
        }
    }
    return tmp;
}

function objDelValue(obj, value) {
    var tmp = {};
    for (var i in obj) {
        if (obj[i] != value) {
            tmp[i] = obj[i]
        }
    }
    return tmp;
}


function arrayDelIndex(array, index) {
    var tmp = [];
    array.map(function (data, i) {
        if (i != index) {
            tmp.push(data)
        }
    })
    return tmp;
}

function arrayDelValue(array, value) {
    var tmp = [];
    array.map(function (data, i) {
        if (data != value) {
            tmp.push(data)
        }
    })
    return tmp;
}
function arrayReplaceValue(array, find,replace) {
    var tmp = [];
    array.map(function (data, i) {
        if (data != find) {
            tmp.push(data)
        }else{
            tmp.push(replace)
        }
    })
    return tmp;
}




//这个是用来创建对象用的，暂时不知道为啥
var Class = { // 全局静态类, 用于声明一个新的类并提供构造函数支持
    create: function() {
        return function() { // 返回一个函数, 代表着这个新声明的类的构造函数
            // 一个命名为initialize的函数将被这个类实现作为类的构造函数
            this.initialize.apply(this, arguments);// initialize函数将在你实例化一个变量的时候被调用执行(即上面7个步骤中的第5步)
        }
    }
}


var PageList = new Class.create();
PageList.prototype = {
    curPage:0,
    noMoreData:0,
    listData:[],
    listBoxId:"",
    apiUrl:"",
    apiParam:{},
    cbFunc:function(res){},
    initialize: function(listBoxId , apiUrl ,apiParam) { // 构造函数
        this.listBoxId = listBoxId
        this.apiUrl = apiUrl
        this.apiParam = apiParam

    },
    getList: function (requestParam) {
        if (requestParam == undefined) {
            requestParam = {}
        }
        var _this = this;
        this.apiParam = requestParam
        requestParam.page = _this.curPage;
        if (_this.noMoreData == 1) {
            showToast("没有更多数据了")
            stopPullDownRefresh()
            stopPullUpLoadMore()
            return;
        }
        apiGet(_this.apiUrl,requestParam, function (res) {
            stopPullDownRefresh()
            stopPullUpLoadMore()
            var data = res.d
            var ModelData = {};
            if (data.length > 0) {
                var listData =  _this.listData.concat(res.d)
                ModelData[_this.listBoxId] = listData
                viewData[_this.listBoxId] = ModelData;
                svd(_this.listBoxId)
                _this.curPage++
                _this.listData = listData
            }else {
                if (_this.curPage == 0) {
                    ModelData[_this.listBoxId] = []
                    viewData[_this.listBoxId] = ModelData;
                    svd(_this.listBoxId)
                }
                _this.noMoreData = 1
                showToast("没有更多数据了")
            }
            _this.cbFunc(res)
        })
    },
    reload: function () {
        this.listData = []
        this.curPage = 0
        this.noMoreData = 0
        this.getList(this.apiParam)
    }
}


function eleWidthAndHeight(ele) {
    var h = $api.dom(ele).offsetHeight;
    var w = $api.dom(ele).offsetWidth;
    return {
        w:w,
        h:h
    }
}

function createForm(en) {
    var form = {};
    logJson(en)
    for(var i in en){
        form["form[" + i +"]"] = en[i]
    }
    if (en.id !== undefined && isTrue(en.id)) {
        form["id"] = en.id
    }
    return form;
}
function recreateEn(item) {
    var data= {}
    for (var i in item) {
        var keys = i.split("_")
        var str = ""
        keys.map(function (data, index) {
            if (index !== 0){
                str += data
            }
        })
        data[ str ] = item[i]
    }
    return data
}

function clearEn(en) {
    var data= {}
    for (var i in en) {
        data[ i ] = ""
    }
    return data
}

function getDate() {
    return new Date().getFullYear() + "-" + ((new Date().getMonth()+1)<10?"0"+(new Date().getMonth()+1):(new Date().getMonth()+1)) + "-" + new Date().getDate()
}
